package org.jeecg.modules.activiti.controller;

import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ProcessDefinition;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.activiti.util.ActUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 流程控制器
 * liuzhize 2019年3月7日下午3:28:14
 */
@Controller
@RequestMapping("act/Definition")
@Slf4j
public class ProcessDefinitionController {

    @Autowired
    private RepositoryService repositoryService;

    @GetMapping("/list")
    @ResponseBody
    public Result<IPage<ProcessDefinition>> list(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        List<ProcessDefinition> models = repositoryService.createProcessDefinitionQuery().latestVersion().listPage((pageNo - 1) * pageSize, pageNo * pageSize);
        JSONArray list=new JSONArray(models);
        IPage page = new Page();
        page.setRecords(list);
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        page.setTotal(repositoryService.createProcessDefinitionQuery().latestVersion().count());
        Result<IPage<ProcessDefinition>> result = new Result();

        result.setResult(page);
        return result;
    }
    @GetMapping("/listAllByKey")
    @ResponseBody
    public Result<IPage<ProcessDefinition>> listAllByKey(@RequestParam String key,@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        List<ProcessDefinition> models = repositoryService.createProcessDefinitionQuery().processDefinitionKey(key).listPage((pageNo - 1) * pageSize, pageNo * pageSize);
        JSONArray list=new JSONArray(models);
        IPage page = new Page();
        page.setRecords(list);
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        page.setTotal(repositoryService.createProcessDefinitionQuery().processDefinitionKey(key).count());
        Result<IPage<ProcessDefinition>> result = new Result();

        result.setResult(page);
        return result;
    }
    @GetMapping("/flowElementList")
    @ResponseBody
    public Result<IPage<FlowElement>> nodeList(@RequestParam(name = "id") String id) {
        BpmnModel bpmnModel = repositoryService.getBpmnModel(id);
        Process mainProcess = bpmnModel.getMainProcess();
        List <FlowElement> flowElements = (List<FlowElement>) mainProcess.getFlowElements();
        IPage page = new Page();
        page.setRecords(flowElements);
        page.setCurrent(1);
        page.setSize(100);
        page.setTotal(flowElements.size());
        Result<IPage<FlowElement>> result = new Result();
        result.setResult(page);
        return result;
    }
    @ResponseBody
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        repositoryService.deleteDeployment(id);
        return Result.ok();
    }
    /**
     * 挂起流程
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/suspendProcessDefinitionById")
    public Result<?> suspendProcessInstanceById(@RequestParam(name = "id") String id) {
        repositoryService.suspendProcessDefinitionById(id);
        return Result.ok();
    }

    /**
     * 激活流程
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/activateProcessDefinitionById")
    public Result<?> activateProcessDefinitionById(@RequestParam(name = "id") String id) {
        repositoryService.activateProcessDefinitionById(id);
        return Result.ok();
    }


}
